package com.icoolkj.learn.juc.future;

import com.icoolkj.learn.juc.base.utils.SleepTools;
import lombok.SneakyThrows;
import lombok.extern.slf4j.Slf4j;

import java.util.Random;
import java.util.concurrent.CompletableFuture;
import java.util.function.Consumer;
import java.util.function.Function;

/**
 * @author icoolkj
 * @version 1.0
 * @description
 * @createDate 2025/03/05 15:39
 */
@Slf4j
public class CFDemo6 {
    @SneakyThrows
    public static void main(String[] args) {
        CompletableFuture<Integer> future1 = CompletableFuture.supplyAsync(() -> {
            int number = new Random().nextInt(10);
            log.info("第一阶段 start：{}", number);
            SleepTools.second(number);
            log.info("第一阶段 end：{}", number);
            return number;
        });

        CompletableFuture<Integer> future2 = CompletableFuture.supplyAsync(() -> {
            int number = new Random().nextInt(10);
            log.info("第二阶段 start：{}", number);
            SleepTools.second(number);
            log.info("第二阶段 end：{}", number);
            return number;
        });

//        future1.applyToEither(future2, new Function<Integer, Integer>() {
//            @Override
//            public Integer apply(Integer number) {
//                log.info("最终结果：{}", number);
//                return number * 2;
//            }
//        }).join();

//        future1.runAfterEither(future2, () ->{
//             log.info("已经有一个任务完成执行");
//        }).join();

//        future1.acceptEither(future2, new Consumer<Integer>() {
//            @Override
//            public void accept(Integer number) {
//                log.info("最终结果：{}", number);
//            }
//        }).join();

//        future1.runAfterBoth(future2, ()->{
//                log.info("上面两个任务都执行完成了");
//        }).join();


//        CompletableFuture<Object> future = CompletableFuture.anyOf(future1, future2);
//        System.out.println(future.join());

        CompletableFuture<Void> future = CompletableFuture.allOf(future1,future2);

        future.get();

        log.info("future1 :{} , future2 :{}", future1.get(), future2.get());

    }
}
